<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 房间延时排钟控制器
 * @package app\index\controller
 */
class RoomDelay extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取延迟排钟列表
     * @return array
     */
    /**
     * 获取当前房间等待项目(首页房间等待用)
     */
    public function waitList()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['limit'])) {
                $getData['limit'] = '10';
            }
            $data = Db::name('qt_wait')
                ->alias('w')
                ->leftJoin('qt_project p', 'p.id = w.project_id')
                ->leftJoin('qt_room r', 'r.id = w.room_id')
                ->field('w.id,w.project_id,p.name as project_name,r.name as room_name,w.create_time,w.create_id,w.delay_time,w.sex,w.number')
                ->where(['w.status'=>'1'])
                ->where('w.delay_time','>',0)
                ->order('w.id desc')
                ->paginate($getData['limit'])->each(function ($item){
                    $item['create_name'] = Db::name('qt_users')->where('id',$item['create_id'])->value('real_name');
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 取消排钟
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            Db::name('qt_wait')->where('id',$getData['id'])->update(['status'=>'2']);
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 安排技师
     */
    public function arrange(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $orderData = Db::name('qt_wait')->where(['id'=>$getData['id']])->find();
            if(empty($orderData)){
                $this->result('无效的延迟排钟', '', 204);
            }
            //$data['man_num'] + $data['woman_num'] + $data['other_num'];
            $orderData['man_num'] = '0';
            $orderData['woman_num'] = '0';
            $orderData['other_num'] = '0';
            if($orderData['sex'] == '1'){
                $orderData['man_num'] = $orderData['number'];
            }
            if($orderData['sex'] == '2'){
                $orderData['woman_num'] = $orderData['number'];
            }
            if($orderData['sex'] == '3'){
                $orderData['other_num'] = $orderData['number'];
            }
            $orderData['hand_card_id'] = '0';
            $orderData['delay'] = 0;
            $orderData['time_type'] = 1;
            $orderData['order_type'] = 1;
            $orderData['create_id'] = $userInfo['id'];
            $orderData['sale_id'] = $orderData['create_id'];
            $orderModel = new \app\common\model\Order();
            $code = $orderModel->create_order($orderData,$userInfo);
            if($code['code'] == '200'){
                Db::name('qt_wait')->where(['id'=>$getData['id']])->update(['status'=>'2']);
                $this->result('下单成功','',200);
            }
        }else{
            $this->result('无效的请求','',204);
        }

    }




}
